package com.misc;

import com.activity.FrdTopicActivity;
import com.activity.MainActivity;
import com.activity.R;
import com.animation.AnimationStepListener;
import com.animation.AnimationTimerTask;
import com.animation.CalculateValue;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.MotionEvent;

import java.util.Timer;

public class OperateFrdLayer implements AnimationStepListener {
	// constant
	final int WIDTH = 296;
	final int HEGIHT = 208;
	final int DEPTH = 20;
	final int ANIMATION_INTERVAL_MILLISECOND = 80;
	final String mood[] = { "Sad", "Happy", "Fine", "Lonely", "Travlling", "Enjoying my work" };

	// field
	Operation operation;
	OnlineFrdView onlineFrdView;
	int  x,  y,  internalWidth;
	int[]  startPos = new int[3],  endPos = new int[3],  pos = new int[3],  srcBitmapPixel;
	Rect[] clickEventArray;
	boolean isRunning;
	
	String phNum;
	String name;
	Bitmap  avatar,  bitmap;
	
	
	public OperateFrdLayer(OnlineFrdView onlineFrdView, String phNum, String name, Bitmap avatar, int x, int y) {
		this.onlineFrdView = onlineFrdView;
		this.phNum = phNum;
		this.name = name;
		this.avatar = avatar;
		
		operation = new Operation();
		bitmap = Bitmap.createBitmap(WIDTH, HEGIHT, Config.ARGB_4444);
		prepareLayerBitmap();
		initialPosition(x, y);
		setupClickEventArea();
	}

	
	void prepareLayerBitmap() {
		Canvas canvas = new Canvas();
		canvas.setBitmap(bitmap);
		canvas.drawColor(Color.BLACK);
		
		Paint paint = new Paint();
		paint.setColor(OnlineFrdView.NAME_TITLE_COLOR);
		canvas.drawRect(100, 47, WIDTH - 6, 97, paint);
		
		paint.setTextSize(20);
		paint.setColor(Color.BLACK);
		canvas.drawText(name,100 + (WIDTH - 100 - paint.measureText(name))/2,80, paint);
		
		canvas.drawBitmap(( (BitmapDrawable) onlineFrdView.getActivity().getResources().getDrawable(R.drawable.up_bar) ).getBitmap(), 0, 0, null);
		canvas.drawBitmap(( (BitmapDrawable) onlineFrdView.getActivity().getResources().getDrawable(R.drawable.down_bar) ).getBitmap(), 0, 145, null);
		
		paint.setColor(OnlineFrdView.PH_NUM_TITLE_COLOR);
		canvas.drawRect(100, 97, WIDTH - 6, 147, paint);
		
		paint.setColor(Color.BLACK);
		// TODO make moods selectable
		String currentMood = mood[2];
		paint.setTextSize(24);
		canvas.drawText(currentMood,100 + (WIDTH - 100 - paint.measureText(currentMood))/2,127, paint);
		
		Matrix matrix = new Matrix();
		matrix.postScale((OnlineFrdView.SL / (float)avatar.getWidth()), OnlineFrdView.SL / (float)avatar.getHeight());
		matrix.postTranslate(1, 47);
		canvas.drawBitmap(avatar, matrix, paint);
		
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		srcBitmapPixel = new int[width * height];
		bitmap.getPixels(srcBitmapPixel, 0, width, 0, 0, width, height);
	}


	void initialPosition(int aX, int aY) {
		startPos[0] = aX;
		startPos[1] = aY;
		startPos[2] = 10;
		x = (320 - WIDTH) / 2;
		y = (480 - HEGIHT) / 2;
		endPos[0] = x;
		endPos[1] = y;
		endPos[2] = WIDTH / 2;
	}

	
	void setupClickEventArea() {
		clickEventArray = new Rect[7];
		int tempWidth = 291 / 3;
		clickEventArray[0] = new Rect(x, y, x + tempWidth, y + 47);
		clickEventArray[1] = new Rect(x + tempWidth + 2, y, x + 2 * tempWidth, y + 47);
		clickEventArray[2] = new Rect(x + 2 * tempWidth + 2, y, x + 3 * tempWidth, y + 47);
		clickEventArray[3] = new Rect(x, y + 49, x + WIDTH, y + 147);
		clickEventArray[4] = new Rect(x, y + 147, x + tempWidth, y + HEGIHT);
		clickEventArray[5] = new Rect(x + tempWidth + 2, y + 147, x + 2 * tempWidth, y + HEGIHT);
		clickEventArray[6] = new Rect(x + 2 * tempWidth + 2, y + 147, x + 3 * tempWidth, y + HEGIHT);
	}

	
	void drawBitmap(Canvas canvas, Paint paint) {
		if (internalWidth > 0 && internalWidth <= WIDTH / 2) {
			int destImage[] = new int[WIDTH * (HEGIHT + 40)];
			Tool.flipZ(destImage, srcBitmapPixel, 20, internalWidth, true, WIDTH, HEGIHT);
			canvas.drawBitmap(destImage, 0, WIDTH, x, y - DEPTH, WIDTH, HEGIHT + (DEPTH << 1), true, paint);
		} else
			canvas.drawBitmap(bitmap, x, y, null);
	}

	
	void handleSingleTapUp(MotionEvent e) {
		switch (getClickedRespondAreaIndex(e)) {
		case 0:
			operation.dial();
			break;
		case 1:
			operation.sms();
			break;
		case 2:
		case 3:
		case 4:
			operation.viewFrdDetail();
			break;
		case 5:
			operation.showLocation();
			break;
		case 6:
			operation.backToOnlineFrdView();
			break;
		default:
			break;
		}
	}


	int getClickedRespondAreaIndex(MotionEvent e) {
		int locationX = (int) e.getX();
		int locationY = (int) e.getY();
		for(int i = 0; i < 7; i++) {
			if (clickEventArray[i].contains(locationX, locationY)) {
				return i;
			}
		}
		return -1;
	}


	void animationEffect() {
		if (!isRunning) {
			isRunning = true;
			Timer tempTimer = new Timer();
			CalculateValue.ECurveType curveType = CalculateValue.ECurveType.InOutCirc;
			AnimationTimerTask animationTimerTask = new AnimationTimerTask(startPos, endPos, curveType, pos, 3, 200, 20, tempTimer, this);
			tempTimer.schedule(animationTimerTask, 0, ANIMATION_INTERVAL_MILLISECOND);
		}
	}


	public void AnimationStep() {
		x = pos[0];
		y = pos[1];
		internalWidth = pos[2];
		onlineFrdView.postInvalidate();
	}

	
	public void AnimationFinished() {
		isRunning = false;
		onlineFrdView.postInvalidate();
	}
	
	

	class Operation {
		void viewFrdDetail() {
			Intent intent = new Intent(onlineFrdView.getActivity(), FrdTopicActivity.class);
			intent.putExtra(MainActivity.FRD_PH_NUM, phNum);
			intent.putExtra(MainActivity.MY_PH_NUM, onlineFrdView.getMe().getPhNum());
			intent.putExtra(MainActivity.MY_NAME, onlineFrdView.getMe().getName());
			intent.putExtra(MainActivity.MY_AVATAR, onlineFrdView.getMe().getAvatar());
			onlineFrdView.getActivity().startActivity(intent);
		}
		
		void dial() {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + phNum));
			onlineFrdView.getActivity().startActivity(callIntent);
		}

		void sms() {
			Uri uri = Uri.parse("smsto:" + phNum);
			Intent messageIntent = new Intent(Intent.ACTION_SENDTO, uri);
			// messageIntent.putExtra("sms_body", message);
			onlineFrdView.getActivity().startActivity(messageIntent);
		}

		void showLocation() {
			/*Intent startLocationIntent = new Intent();
			startLocationIntent.setClass(mainPage, LocationActivity.class);
			Bundle landLbundle = new Bundle();
			// only simulate the location.
			Random rnd = new Random();
			int locationindex = Math.abs(rnd.nextInt()) % longitude.length;
			landLbundle.putInt("LONGITUDE", (int) (latitude[locationindex] * 1000000));
			landLbundle.putInt("LATITUDE", (int) (longitude[locationindex] * 1000000));
			startLocationIntent.putExtras(landLbundle);
			mainPage.startActivity(startLocationIntent);*/
		}

		void backToOnlineFrdView() {
			onlineFrdView.setIsOperateFrdLayerOn(false);
			System.gc();
			onlineFrdView.getActivity().setContentView(onlineFrdView);
		}
	}
}
